package com.bdbit.ChargingStation.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

public class OrderEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long id;                    // 订单ID
    private String userId;              // 用户ID
    private String orderYearMonth;      // 订单创建时间（年月份）
    private Integer status;             // 订单状态：0-未支付,1-进行中,2-已完成,3-已退款
    private String pileId;              // 充电桩ID
    private String regionCode;          // 区域编码
    private String eleCode;             // 电桩号
    private BigDecimal chargingKwh;     // 充电量（千瓦时）
    private BigDecimal unitPrice;       // 充电单价（元/千瓦时）
    private BigDecimal totalAmount;     // 订单总金额
    private Date startTime;             // 充电开始时间
    private Date endTime;               // 充电结束时间
    private Integer reportedDuration;   // 上报充电时长（分钟）
    private Date deviceStartTime;       // 设备上报开始时间
    private Date deviceEndTime;         // 设备上报结束时间
    private BigDecimal prepayAmount;    // 订单预付金额
    private String paymentMethod;       // 支付方式：WECHAT,ALIPAY,UNIONPAY
    private Date paymentTime;           // 支付时间
    private BigDecimal refundAmount;    // 退款金额
    private Date refundTime;            // 退款时间
    private Date createDate;            // 订单创建日期（年月日）
    private java.sql.Date updateDate;   // 订单更新日期（年月日）- 注意这是java.sql.Date类型
    private Integer isDeleted;          // 逻辑删除：0-未删除,1-已删除
    private String payType;             // 支付方式：wxpay/alipay
    private String paymentForm;         // 支付表单数据
    private Integer chargingMinutes;    // 充电分钟数
    private String orderId;             // 订单ID字符串

    // 状态常量 - 与ChargingOrder.java保持一致
    public static final int STATUS_UNPAID = 0;           // 未支付
    public static final int STATUS_PAID_WAITING = 1;     // 已支付待充电
    public static final int STATUS_CHARGING = 2;         // 充电中
    public static final int STATUS_COMPLETED = 3;        // 充电完成
    public static final int STATUS_REFUNDED = 4;         // 已退款
    public static final int STATUS_TIMEOUT = 5;          // 超时
    public static final int STATUS_CANCELLED = 6;        // 已取消

    public static final int DELETED_NO = 0;  // 未删除
    public static final int DELETED_YES = 1; // 已删除

    // 无参构造器
    public OrderEntity() {
    }

    // Getter和Setter方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getOrderYearMonth() {
        return orderYearMonth;
    }

    public void setOrderYearMonth(String orderYearMonth) {
        this.orderYearMonth = orderYearMonth;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getPileId() {
        return pileId;
    }

    public void setPileId(String pileId) {
        this.pileId = pileId;
    }

    public String getRegionCode() {
        return regionCode;
    }

    public void setRegionCode(String regionCode) {
        this.regionCode = regionCode;
    }

    public String getEleCode() {
        return eleCode;
    }

    public void setEleCode(String eleCode) {
        this.eleCode = eleCode;
    }

    public BigDecimal getChargingKwh() {
        return chargingKwh;
    }

    public void setChargingKwh(BigDecimal chargingKwh) {
        this.chargingKwh = chargingKwh;
    }

    public BigDecimal getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(BigDecimal unitPrice) {
        this.unitPrice = unitPrice;
    }

    public BigDecimal getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Integer getReportedDuration() {
        return reportedDuration;
    }

    public void setReportedDuration(Integer reportedDuration) {
        this.reportedDuration = reportedDuration;
    }

    public Date getDeviceStartTime() {
        return deviceStartTime;
    }

    public void setDeviceStartTime(Date deviceStartTime) {
        this.deviceStartTime = deviceStartTime;
    }

    public Date getDeviceEndTime() {
        return deviceEndTime;
    }

    public void setDeviceEndTime(Date deviceEndTime) {
        this.deviceEndTime = deviceEndTime;
    }

    public BigDecimal getPrepayAmount() {
        return prepayAmount;
    }

    public void setPrepayAmount(BigDecimal prepayAmount) {
        this.prepayAmount = prepayAmount;
    }

    public String getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    public Date getPaymentTime() {
        return paymentTime;
    }

    public void setPaymentTime(Date paymentTime) {
        this.paymentTime = paymentTime;
    }

    public BigDecimal getRefundAmount() {
        return refundAmount;
    }

    public void setRefundAmount(BigDecimal refundAmount) {
        this.refundAmount = refundAmount;
    }

    public Date getRefundTime() {
        return refundTime;
    }

    public void setRefundTime(Date refundTime) {
        this.refundTime = refundTime;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public java.sql.Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(java.sql.Date updateDate) {
        this.updateDate = updateDate;
    }

    public Integer getIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(Integer isDeleted) {
        this.isDeleted = isDeleted;
    }

    public String getPayType() {
        return payType;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }
    
    public String getPaymentForm() {
        return paymentForm;
    }

    public void setPaymentForm(String paymentForm) {
        this.paymentForm = paymentForm;
    }

    public Integer getChargingMinutes() {
        return chargingMinutes;
    }

    public void setChargingMinutes(Integer chargingMinutes) {
        this.chargingMinutes = chargingMinutes;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    // 业务方法
    public BigDecimal calculateActualAmount() {
        if (chargingKwh != null && unitPrice != null) {
            return chargingKwh.multiply(unitPrice);
        }
        return BigDecimal.ZERO;
    }

    public BigDecimal calculateRefundAmount() {
        if (prepayAmount != null && totalAmount != null) {
            return prepayAmount.subtract(totalAmount);
        }
        return BigDecimal.ZERO;
    }

    public long calculateChargingDuration() {
        if (startTime != null && endTime != null) {
            return (endTime.getTime() - startTime.getTime()) / (1000 * 60);
        }
        return 0;
    }

    public boolean isValid() {
        return isDeleted == DELETED_NO &&
                chargingKwh != null && chargingKwh.compareTo(BigDecimal.ZERO) > 0 &&
                unitPrice != null && unitPrice.compareTo(BigDecimal.ZERO) > 0;
    }

    public String getStatusText() {
        if (status == null) return "未知状态";

        switch (status) {
            case STATUS_UNPAID: return "未支付";
            case STATUS_PAID_WAITING: return "已支付待充电";
            case STATUS_CHARGING: return "充电中";
            case STATUS_COMPLETED: return "充电完成";
            case STATUS_REFUNDED: return "已退款";
            case STATUS_TIMEOUT: return "超时";
            case STATUS_CANCELLED: return "已取消";
            default: return "未知状态";
        }
    }

    @Override
    public String toString() {
        return "OrderEntity{" +
                "id=" + id +
                ", userId='" + userId + '\'' +
                ", status=" + status +
                ", eleCode='" + eleCode + '\'' +
                ", totalAmount=" + totalAmount +
                ", startTime=" + startTime +
                ", endTime=" + endTime +
                ", prepayAmount=" + prepayAmount +
                ", paymentMethod='" + paymentMethod + '\'' +
                '}';
    }

    public void setOrder_status(int i) {
        this.status = i;
    }

    // 获取订单ID（字符串形式）
    public String getOrderId() {
        // 优先使用orderId字段
        if (orderId != null) {
            return orderId;
        }
        // 如果orderId为null，则使用id字段转为字符串
        return id != null ? id.toString() : null;
    }

    // 添加缺少的getPayTime方法
    public Date getPayTime() {
        return paymentTime; // 使用已有的paymentTime字段
    }
}